import '../app_configure.dart';
import 'device_data_base.dart';

class DeviceDataV21 extends DevcieDataBase {
  final List<int> datas = [0xA4, 0x40, 0x00, 0x00, 0x42, 0x4C, 0x4B, 0x5B]; //对码
  final List<int> cdats = [
    0x40,
    0x40,
    0x00,
    0x00,
    0x80,
    0x80,
    0x80,
    0x80,
    0x00,
    0xBF
  ]; //控制

  DeviceDataV21() {
    fillAppId();
  }

  @override
  void fillAppId() {
    datas[2] = AppConfig.instance.appId & 0xFF;
    datas[3] = (AppConfig.instance.appId >> 8) & 0xFF;

    cdats[2] = AppConfig.instance.appId & 0xFF;
    cdats[3] = (AppConfig.instance.appId >> 8) & 0xFF;
  }

  @override
  List<int> getPair() {
    cdats[4] = 0x80;
    cdats[5] = 0x80;
    cdats[6] = 0x80;
    cdats[7] = 0x80;
    return datas;
  }

  @override
  List<int> getStopPair() {
    cdats[4] = 0x80;
    cdats[5] = 0x80;
    cdats[6] = 0x80;
    cdats[7] = 0x80;
    return cdats;
  }

  @override
  List<int> getSpeed(int i) {
    cdats[4] = 0xFF & i;
    return cdats;
  }

  @override
  List<int> getDirection(int i) {
    cdats[3] = 0xFF & i;
    return cdats;
  }

  @override
  List<int> getDataAt(int index, int value) {
    if (index < 0 || index > (cdats.length - 1)) {
      return cdats;
    }
    cdats[index] = 0xFF & value;
    return cdats;
  }

  @override
  List<int> actionStop() {
    cdats[4] = 0x80;
    cdats[5] = 0x80;
    cdats[6] = 0x80;
    cdats[7] = 0x80;
    return cdats;
  }

  //前进
  @override
  List<int> actionForward({bool reverse = false, double ratio = 1}) {
    if (reverse) {
      // getDataAt(7, 0x98);
      return getDataAt(4, 0x98);
    }
    // getDataAt(7, 24);
    return getDataAt(4, 24);
  }

  //后退
  @override
  List<int> actionBackward({bool reverse = false, double ratio = 1}) {
    if (reverse) {
      // getDataAt(7, 24);
      return getDataAt(4, 24);
    }
    // getDataAt(7, 0x98);
    return getDataAt(4, 0x98);
  }

  //左转
  @override
  List<int> actionLeft({bool reverse = false, double ratio = 1}) {
    if (reverse) {
      return getDataAt(7, 0x98);
      // return getDataAt(4, 24);
    }
    return getDataAt(7, 24);
    // return getDataAt(4, 0x98);
  }

  //右转
  @override
  List<int> actionRight({bool reverse = false, double ratio = 1}) {
    if (reverse) {
      return getDataAt(7, 24);
      // return getDataAt(4, 0x98);
    }
    return getDataAt(7, 0x98);
    // return getDataAt(4, 24);
  }

  //左转前进
  @override
  List<int> actionForwardLeft(
      {bool verticalReverse = false, bool horizontalReverse = false}) {
    if (verticalReverse) {
      if (horizontalReverse) {
        getDataAt(7, 0x98);
        return getDataAt(4, 0x98);
      } else {
        getDataAt(7, 0x98);
        return getDataAt(4, 9);
      }
    } else {
      if (horizontalReverse) {
        getDataAt(7, 9);
        return getDataAt(4, 24);
      } else {
        getDataAt(7, 24);
        return getDataAt(4, 9);
      }
    }
  }

  //右转前进
  @override
  List<int> actionForwardRight(
      {bool verticalReverse = false, bool horizontalReverse = false}) {
    if (verticalReverse) {
      if (horizontalReverse) {
        getDataAt(7, 0x89);
        return getDataAt(4, 0x98);
      } else {
        getDataAt(7, 9);
        return getDataAt(4, 0x98);
      }
    } else {
      if (horizontalReverse) {
        getDataAt(7, 24);
        return getDataAt(4, 9);
      } else {
        getDataAt(7, 9);
        return getDataAt(4, 24);
      }
    }
  }

  //左转后退
  @override
  List<int> actionBackwardLeft(
      {bool verticalReverse = false, bool horizontalReverse = false}) {
    if (verticalReverse) {
      if (horizontalReverse) {
        getDataAt(7, 24);
        return getDataAt(4, 9);
      } else {
        getDataAt(7, 0x98);
        return getDataAt(4, 9);
      }
    } else {
      if (horizontalReverse) {
        getDataAt(7, 0x89);
        return getDataAt(4, 0x98);
      } else {
        getDataAt(7, 0x98);
        return getDataAt(4, 0x98);
      }
    }
  }

  //右转后退
  @override
  List<int> actionBackwardRight(
      {bool verticalReverse = false, bool horizontalReverse = false}) {
    if (verticalReverse) {
      if (horizontalReverse) {
        getDataAt(7, 9);
        return getDataAt(4, 24);
      } else {
        getDataAt(7, 0x89);
        return getDataAt(4, 24);
      }
    } else {
      if (horizontalReverse) {
        getDataAt(7, 0x98);
        return getDataAt(4, 0x98);
      } else {
        getDataAt(7, 0x89);
        return getDataAt(4, 0x98);
      }
    }
  }
}

/**
BLK055Rx-APP协议
对码数据：
data[0]:0xA4
data[1]:0x40
data[2]:APP_ID[0]
data[3]:APP_ID[1]
data[4]:
data[5]:
data[6]:
data[7]:0x5B


控制数据：
data[0]:0x40
data[1]:0x40
data[2]:APP_ID[0]
data[3]:APP_ID[1]
data[4]:停止【0x80】，前进【 小于0x80】，后退【 大于0x80】
data[5]:
data[6]:松手【0x80】，LED1长按切换模式【 0x00】，LED2长按切换模式【 0xFF】
data[7]:停止【0x80】，左转【 小于0x80】，右转【 大于0x80】
data[8]:
data[9]:0xBF


 */